package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("qo",qo);
        return "employee/list";
    }

    @RequiredPermission(name = "删除员工",expression = "employee.delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        //把部门传到添加页面,用于选择部门
        model.addAttribute("departments",departmentService.selectAll());
        //把权限传员工添加页面,让员工选择角色
        model.addAttribute("roles",roleService.selectAll());
        //通过员工id查询员工对应的角色
        model.addAttribute("selfRoles",roleService.selectByRoleRelation(id));
        if (id != null){
            Employee employee = employeeService.selectById(id);
            model.addAttribute("employee",employee);
        }
        return "employee/input";
    }

    @RequiredPermission(name = "员工新增/编辑",expression = "employee.saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId() != null){
            employeeService.update(employee,ids);
        }else {
            employeeService.save(employee,ids);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置下载响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建文本对象
        Workbook wb = employeeService.exportXls();
        //把文本响应到浏览器中
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }
}
